package com.mrobuy.jd.category;

import com.gavel.database.SQLExecutor;
import com.jd.open.api.sdk.JdClient;
import com.mrobuy.jd.JdClientFactory;
import com.mrobuy.jd.api.Jos;
import com.mrobuy.jdvc.entity.VCCategory;
import com.mrobuy.jdvc.entity.VCCategoryExtProp;
import com.mrobuy.jdvc.entity.VCExtPropValue;
import com.mrobuy.util.IdentifierGen;

import java.util.List;

public class VCCategoryExtPropsLoader {

    public static void main(String[] args) throws Exception {

        List<VCCategory> categories = SQLExecutor.executeQueryBeanList("select * from VC_CATEGORY ", VCCategory.class);

        for (VCCategory category : categories) {

            System.out.println(category);

            load(category.getId(), JdClientFactory.jdClient());

        }

    }

    public static void load(int cid, JdClient client) throws Exception {

        SQLExecutor.execute("delete from VC_CATEGORY_EXTPROP where CID = ?", cid);

        SQLExecutor.execute("delete from VC_EXTPROP_VALUE where CID = ?", cid);


        List<VCCategoryExtProp>  props = Jos.extPropFindClient(client)
                .setCid3(cid)
                .call();

        for (VCCategoryExtProp prop : props) {

            prop.setId(IdentifierGen.uuid());
            SQLExecutor.insert(prop);

            if ( prop.getOptions()!=null ) {
                for (VCExtPropValue propValue : prop.getOptions()) {
                    propValue.setId(IdentifierGen.uuid());
                    SQLExecutor.insert(propValue);
                }
            }

        }
    }

}
